<template>
  <div class="page-style">
    <btn-area @reloadFun="reloadFun" @download="downloadFun" @search="searchFun" @upDown="statusChange" />
    <tab-list :tab-data="brandProductList" @reloadFun="reloadFun" />
    <pagination :total="total" @pagination="pagination" />
  </div>
</template>

<script>
import btnArea from './components/product-list/product-btn'
import tabList from './components/product-list/list-tab'
import pagination from '@/components/Pagination'
import { getProductList } from '@/api/brand'
import { timestampToDate } from '@/utils/index'
export default {
  components: {
    btnArea,
    tabList,
    pagination
  },
  data() {
    return {
      total: 0,
      p: 1,
      num: 10,
      brand_product_name: '',
      brand_id: this.$store.getters.brand_id,
      brandProductList: [],
      status: 1,
      brand_list: []
    }
  },
  created() {
    this.getProductList()
  },
  methods: {
    statusChange(val) {
      this.status = val
      this.getProductList()
    },
    pagination(val) {
      this.p = val.page
      this.num = val.limit
      this.getProductList()
    },
    getProductList() {
      const data = {
        p: this.p,
        num: this.num,
        brand_id: this.brand_id,
        status: this.status,
        brand_product_name: this.brand_product_name
      }
      getProductList(data).then(res => {
        this.total = Number(res.count)
        this.brandProductList = res.list
      })
    },
    reloadFun() {
      this.getProductList()
    },
    searchFun(val) {
      console.log(val)
      this.brand_id = val.brand_id
      this.brand_product_name = val.brand_product_name
      this.getProductList()
    },
    downloadFun() {
      const data = []
      const list = this.brandProductList
      for (const i in list) {
        const arr = []
        arr.push(list[i].brand_content.brand_name, ' ' + list[i].product_code_num, list[i].brand_product_name, list[i].gg, list[i].pany, list[i].price)
        data.push(arr)
      }
      const time = Date.parse(new Date()) / 1000
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header: ['授权品牌名', '产品代码', '产品名称', '规格', '单位', '统一售价'],
          filename: '产品列表' + timestampToDate(time, 10),
          autoWidth: true,
          data: data
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
